package org.example.clouddemo.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.clouddemo.bean.common.entity.UserWallet;
import org.example.clouddemo.bean.common.param.LoginParam;
import org.example.clouddemo.bean.common.param.RegisterParam;
import org.example.clouddemo.bean.common.vo.LoginResultVo;
import org.example.clouddemo.bean.common.vo.UserAppVo;
import org.example.clouddemo.service.common.UserService;
import org.example.clouddemo.service.common.mapper.UserWalletMapper;
import org.example.common.annotation.IgnoreAuth;
import org.example.common.auth.TokenService;
import org.example.common.bean.LoginUser;
import org.example.common.response.ApiResult;
import org.example.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;


/**
 * <p>
 * 用户表 内部接口
 * </p>
 *
 * @author hzg
 * @since 2025-06-06
 */

@Api(tags = "用户接口")
@ApiSupport(order = 1)
@RestController("appUserController")
@RequestMapping("/api/app/user/")
public class UserController {

    @Autowired
    public UserService userService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private UserWalletMapper userWalletMapper;

    @ApiOperation(value = "用户注册", notes = "不需要token")
    @ApiOperationSupport(order = 1)
    @PostMapping("register")
    @IgnoreAuth
    public ApiResult<Long> register(@Validated @ModelAttribute RegisterParam param, @RequestParam("file") MultipartFile file, HttpServletRequest request) {
        return ApiResult.success(userService.register(param,file,request));
    }


    @ApiOperation(value = "用户登录", notes = "不需要token")
    @ApiOperationSupport(order = 2)
    @PostMapping("login")
    @IgnoreAuth
    public ApiResult<LoginResultVo> login(@Validated @RequestBody LoginParam param) {

        return ApiResult.success(userService.login(param));
    }


    @ApiOperation(value = "获取用户信息", notes = "需要token")
    @ApiOperationSupport(order = 3)
    @PostMapping("getInfo")
    public ApiResult<UserAppVo> getInfo() {

        LoginUser loginUser = SecurityUtils.getUserInfo();
        Long userId = loginUser.getId();

        //获取用户信息
        UserAppVo userAppVo = userService.getInfo(userId);

        return ApiResult.success(userAppVo);
    }


    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 4)
    @PostMapping(value = "logout")
    public ApiResult logout() {
        LoginUser loginUser = SecurityUtils.getUserInfo();
        tokenService.delLoginUser(String.valueOf(loginUser.getId()), loginUser.getUuid(), LoginUser.USER_TYPE_APP);

        return ApiResult.success();
    }

    @ApiOperation(value = "修改用户头像", notes = "需要token")
    @ApiOperationSupport(order = 5)
    @PostMapping("updateAvatar")
    public ApiResult<Boolean> updateAvatar(@RequestParam("file") MultipartFile file,
                                           HttpServletRequest request) {

        // 调用 userService 方法处理头像更新
        Boolean result = userService.updateAvatar(file,request);

        return ApiResult.success(result);
    }

    @ApiOperation(value = "修改用户昵称", notes = "需要token")
    @ApiOperationSupport(order = 6)
    @PostMapping("updateNickname")
    public ApiResult<Boolean> updateNickname(@RequestParam("nickname") String nickname) {
        Boolean result = userService.updateNickname(nickname);

        return ApiResult.success(result);
    }
    /**
     * 查询当前用户的钱包余额
     */
    @GetMapping("balance")
    public ApiResult<BigDecimal> getBalance() {
        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();
        QueryWrapper<UserWallet> queryWrapper = new QueryWrapper<UserWallet>().eq("user_id", userId);
        // 查询余额
        BigDecimal balance = userWalletMapper.selectOne(queryWrapper).getBalance();

        return ApiResult.success(balance);
    }

    @PostMapping("recharge")
    public ApiResult<Boolean> recharge(@RequestParam("amount") BigDecimal amount) {
        // 参数校验
        if (amount == null || amount.compareTo(BigDecimal.ZERO) <= 0) {
            return ApiResult.error("充值金额必须大于0");
        }

        // 获取当前登录用户ID
        Long userId = SecurityUtils.getUserId();

        // 执行充值逻辑
        Boolean result = userService.recharge(userId, amount);

        return ApiResult.success(result);
    }


}
